﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum EWindowLayer
{
    None = 0,
    HUD,
    FULL,
    WINDOWS,
    TOP,
    DRAMA,
    TIP,
    GM,
}

public sealed class UIManager : MonoSingleton<UIManager>
{

    private UIRoot uiRoot = null;

    private Dictionary<Type, List<UIWindow>> openedWindow = new Dictionary<Type, List<UIWindow>>();

    private void Awake()
    {
        uiRoot = LoadUI<UIRoot>(null);
    }

    public T LoadUI<T>(Transform inParent) where T : UIBase
    {
        UIAssetInfoAttribute attrInfo = Utils.GetAttributeInfo<T>();
        if(null == attrInfo) {
            Debug.LogErrorFormat("UIAssetInfoAttribute is not found in {0}.", typeof(T).FullName);
            return null;
        }

        GameObject obj = AssetsPoolManager.Instance.LoadAsset(attrInfo.BundelName, attrInfo.AssetName);
        if(null != inParent) {
            obj.transform.SetParent(inParent);
        } else {
            // UIRoot: don't need set parent
            obj.name = attrInfo.AssetName;
        }

        return obj.GetComponent<T>();
    }

    public T LoadUIAsync<T>() where T : UIBase
    {
        return default(T);
    }

    private Transform getWindowParent(EWindowLayer wLayer)
    {
        return null;
    }

    public T ShowWindow<T>() where T : UIWindow
    {
        Type wType = typeof(T);
        UIAssetInfoAttribute attrInfo = Utils.GetAttributeInfo<T>();
        if (null == attrInfo) {
            Debug.LogErrorFormat("UIAssetInfoAttribute is not found in {0}.", wType.FullName);
            return null;
        }

        GameObject obj = AssetsPoolManager.Instance.LoadAsset(attrInfo.BundelName, attrInfo.AssetName);
        T win = obj.GetComponent<T>();
        if(wType != typeof(UIRoot)) {
            Transform parent = uiRoot.GetWindowParent(win.ELayer);
            obj.transform.SetParent(parent, false);
        }

        if (!openedWindow.ContainsKey(wType)) {
            openedWindow.Add(wType, new List<UIWindow>());
        }
        openedWindow[wType].Add(win);

        return win;
    }

    public T ShowWindowAsync<T>() where T : UIBase
    {
        return default(T);
    }

    public void HideWindow<T>(GameObject inObj = null) where T : UIWindow
    {
        Type type = typeof(T);
        if (!openedWindow.ContainsKey(type)) {
            Debug.LogWarningFormat("Window {0} is not opended.", type.ToString());
            return;
        }
        if(null == inObj) {
            foreach(UIWindow window in openedWindow[type]) {
                AssetsPoolManager.Instance.Collection<T>(window.gameObject);
            }
            openedWindow.Clear();
        } else if(openedWindow[type].Contains(inObj.GetComponent<T>())) {
            openedWindow[type].Remove(inObj.GetComponent<T>());
            AssetsPoolManager.Instance.Collection<T>(inObj);
        } else {
            Debug.LogErrorFormat("Window {0} in not opened.", type.ToString());
        }
    }

    public T FindWindow<T>() where T : UIBase
    {

        return default(T);
    }

}
